
window.activeMultiLab = "";
window.activeMultiKey = "";
window.activeColObj = null;
window.activeRowObj = null;
/* 自动纠正编码错误 - 特殊情况使用 */
window.isMultipleAutoError = true;
window.multiGridTimer = null; 

Yacon.widget.grid.multiple.MultipleGrid = function (conText)
{
	this.elementID          = "";
	this.elementType        = "multipleGrid";
		
	Yacon.apply(this,conText);
	this.Create();
};

Yacon.widget.grid.multiple.MultipleGrid.Extend(Object,{
	
	Create:function()
	{
			    	     
	     this.elementDiv     = document.getElementById("div_"+this.elementID);
	     this.element        = document.getElementById(this.elementID);
	     if (!this.element ||!this.elementDiv )
	    	 return ;
	     
	     this.elementDiv.Control = this;
	     this.element.Control    = this;
	          
	     this.setWidth();
	     this.setDataSet();
	      
	     addResizeElement(this.element);
	},
	
	/* refresh 后 构造方法 */
	afterCreate:function(){
		this.elementDiv     = document.getElementById("div_"+this.elementID);
	     this.element        = document.getElementById(this.elementID);
	     if (!this.element ||!this.elementDiv )
	    	 return ;
	     
	     this.elementDiv.Control = this;
	     this.element.Control    = this;
	          
	     this.setWidth();
	      
	     addResizeElement(this.element);
	},
	
	setDataSet:function (dataSet)
	{	
		if (!dataSet)
		{
			var dataSetID = this.element.getAttribute("id") + "_dataSet";		
			if(this.element.getAttribute("dataSet") && this.element.getAttribute("dataSet") !="")
				 dataSetID = this.element.getAttribute("dataSet");
			if (!isExitDataset(dataSetID))
			{
				this.dataSet   =  createDataset(dataSetID);	
			}else
				this.dataSet   =  getDatasetByID(dataSetID);
		}else
			this.dataSet = dataSet;		
		
		if (this.dataSet)
		{
			this.dataSet.regTable(this);
			addElement(this.element);
			
			if (window.parameter)
			{
				for(var name in window.parameter)
				{
					if (!name.isEqual("logid"))
						this.dataSet.setForeignField(name,window.parameter[name]);
				}
			}
		}
	},
	onresize:function()
	{
		
		if (document.body.offsetWidth>this.element.offsetWidth+50 ||document.body.offsetWidth<this.element.offsetWidth -10)
		{
			this.setWidth();
		}
	},
	setHeight:function(gridHeight)
	{
		if (gridHeight)
			return;
		
		this.gridHeight = gridHeight;
		this.elementDiv.style.height = gridHeight;    
	          
	   if (this.element.offsetHeight < this.elementDiv.offsetHeight)
	   {
	      var offsetHeights = (this.elementDiv.offsetHeight - this.element.offsetHeight)/this.element.rows.length;
	      for (var i=0 ; i<this.element.rows.length;i++)
	      {
	          this.element.rows[i].cells[this.element.rows[i].cells.length/2].style.height = this.element.rows[i].offsetHeight + offsetHeights;
	      }	      
	   }  
	},
	setWidth :function(gridWidth)
	{
	    if (!gridWidth)  
	    	this.gridWidth = document.body.offsetWidth - 5;
	 
	    this.elementDiv.style.width = this.gridWidth+"px";
	    this.element.style.width = this.gridWidth+"px";
	},
	addFieldValue:function(fieldName,fieldValue)
	{
		var field = new Object();
		field.fieldName  = fieldName;
		field.fieldValue = fieldValue;
		  
		this.record[this.record.length] = field;
	},	 
	setRefreshMethod:function(refreshMethod)
	{
		this.dataSet.setRefreshMethod(refreshMethod);
	},
	getFieldByRow:function(fieldRow,fieldIndex)
	{
	    if (!fieldIndex||!fieldRow)
	    	return;
	    
	    var field = null;
	    for(var i=0; i< fieldRow.cells.length; i++)
	    {
	        if (typeof(fieldRow.cells[i].fieldName) == "string")
	        {
	        	if (typeof(fieldIndex) == "string")
	        	{
	        		if (fieldRow.cells[i].fieldName.toLowerCase() == fieldIndex.toLowerCase())
	        		{
	        		  field = fieldRow.cells[i];
		              break;
	        		}
	        	}		          
	        	if (typeof(fieldIndex) == "number")
	        	{
	        		if (fieldRow.cells[i].cellIndex == getInt(fieldIndex))
	        		{
	        			field = fieldRow.cells[i];
	        			break;
		           }
	        	}
	        }
	    }
	    return field;
	},
	getField:function(fieldIndex)
	{
	    if(!this.activeRow)
	    {
	      return null;
	    }
	    return this.getFieldByRow(this.activeRow,fieldIndex);
	},	
	getFieldValue:function(fieldIndex)
	{
	    var field = this.getField(fieldIndex);
	    if (field)
	    {
	      return field.fieldValue;
	    }
	    else
	    {
	      return "";
	    }
	},
	setActiveCell:function(cell)
	{
		if (!cell)
			return;
		
		if(this.activeCell)
			this.activeCell.className = "";
	
		this.activeCell = cell;
	},
	setActiveRow:function(row)
	{
		if (!row)
			return;
		
		if(this.activeRow)
			this.activeRow.className = "";
		
		this.activeRow = row;	  
	},
	setActiveCol:function(col){
		if (!row)
			return;
		
		if(this.activeRow)
			this.activeRow.className = "";
		
		this.activeCol = col;
	},
	/**
	 *  刷新状态
	 */
	refresh:function()
	{
		this.dataSet.refresh();
	},

	afterRefresh:function()
	{
		if (this.dataSet.getResult())
			this.elementDiv.innerHTML = this.dataSet.getDataValue();
	
		this.afterCreate();
		
//		this.Create();
		
		if (Yacon.isfireuserEvent(this.elementID+"_afterRefresh"))
		{
			Yacon.fireUserEvent(this.elementID+"_afterRefresh",[]);
		}
		
	},
	
	clearRow:function()
	{
		
	}
		
	
})	


Yacon.widget.grid.multiple.MultipleGrid.events = {
		
	onCellMouseOut:function()
	{
/*	 	var colObj   = Yacon.getTD(event.srcElement);
	 	var rowObj   = Yacon.getTR(event.srcElement);
	 	colObj.className = "";
	 	rowObj.className = "";*/
	},
	onCellMouseOver:function()
	{
/*	 	var colObj   = Yacon.getTD(event.srcElement);
	 	var rowObj   = Yacon.getTR(event.srcElement);
	 	colObj.className = "mulgridCellSelected";
		rowObj.className = "mulgridrowSelected"*/;
		
	},
	onCellClick:function()
	{
		
		window.multiEvent = event;
		
		clearTimeout(window.multiGridTimer);
		window.multiGridTimer = setTimeout(function () {              
          
			/* 释放上次信息 */
			if(!Yacon.isNull(activeColObj)){
				activeColObj.className = "";
			}
			if(!Yacon.isNull(activeRowObj)){
				activeRowObj.className = "";
			}
			
			var colObj   = Yacon.getTD(window.multiEvent.srcElement);
		 	var rowObj   = Yacon.getTR(window.multiEvent.srcElement);
		 	var TableObj   = Yacon.getTable(rowObj);
		    TableObj.Control.setActiveRow(rowObj);
		    TableObj.Control.setActiveCell(colObj);
		    
		 	/*colObj.className = "mulgridCellSelected";*/
		    colObj.className = "";
			rowObj.className = "mulgridrowSelected";
			
			/* 赋值本次信息 */
			activeColObj = colObj;
			activeRowObj = rowObj;
		    
			activeMultiLab = "";
			activeMultiKey = "";
		    for(var i in rowObj.childNodes){
		    	if(typeof(rowObj.childNodes[i])=="object"){
			    	var fieldVal = rowObj.childNodes[i].getAttribute("fieldKey");
			    	
			    	if(isMultipleAutoError){
				    	if(fieldVal == undefined){
				    		fieldVal = rowObj.childNodes[i].innerHTML;
				    	}
			    	}
			    	
			    	var fieldKey = rowObj.childNodes[i].getAttribute("fieldName");
			    	if(!Yacon.isNull(fieldVal)){
			    		activeMultiLab = activeMultiLab + fieldVal + ",";
			    	}
			    	if(!Yacon.isNull(fieldKey)){
			    		activeMultiKey = activeMultiKey + fieldKey + ",";
			    	}
		    	}
		    };
		    
		    if (Yacon.isfireuserEvent(TableObj.Control.elementID+"_onCellClick"))
				Yacon.fireUserEvent(TableObj.Control.elementID+"_onCellClick",[TableObj]);
			
			
        }, 300);
		

	},
	
	onCellDoubleClick:function()
	{
		clearTimeout(window.multiGridTimer); 
		
		/* 释放上次信息 */
		if(!Yacon.isNull(activeColObj)){
			activeColObj.className = "";
		}
		if(!Yacon.isNull(activeRowObj)){
			activeRowObj.className = "";
		}
		
		var colObj   = Yacon.getTD(event.srcElement);
	 	var rowObj   = Yacon.getTR(event.srcElement);
	 	var TableObj   = Yacon.getTable(rowObj);
	    TableObj.Control.setActiveRow(rowObj);
	    TableObj.Control.setActiveCell(colObj);
	    
	 	/*colObj.className = "mulgridCellSelected";*/
	    colObj.className = "";
		rowObj.className = "mulgridrowSelected";
		
		/* 赋值本次信息 */
		activeColObj = colObj;
		activeRowObj = rowObj;
	    
		activeMultiLab = "";
		activeMultiKey = "";
	    for(var i in rowObj.childNodes){
	    	if(typeof(rowObj.childNodes[i])=="object"){
		    	var fieldVal = rowObj.childNodes[i].getAttribute("fieldKey");
		    	
		    	if(isMultipleAutoError){
			    	if(fieldVal == undefined){
			    		fieldVal = rowObj.childNodes[i].innerHTML;
			    	}
		    	}
		    	
		    	var fieldKey = rowObj.childNodes[i].getAttribute("fieldName");
		    	if(!Yacon.isNull(fieldVal)){
		    		activeMultiLab = activeMultiLab + fieldVal + ",";
		    	}
		    	if(!Yacon.isNull(fieldKey)){
		    		activeMultiKey = activeMultiKey + fieldKey + ",";
		    	}
	    	}
	    };
	    
	    if (Yacon.isfireuserEvent(TableObj.Control.elementID+"_onCellDoubleClick"))
			Yacon.fireUserEvent(TableObj.Control.elementID+"_onCellDoubleClick",[TableObj]);
	}
	
	
};



